﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace UcAsp.Net.BLL
{
    public class AdvConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        public static List<UcAsp.Net.Model.AdvPositions> GetListAdvPositions
        {
            get
            {
                List<UcAsp.Net.Model.AdvPositions> list = new List<UcAsp.Net.Model.AdvPositions>();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\AdvPositions.xml");
                string xmlpath = ("root/AdvPositions");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.AdvPositions model = new UcAsp.Net.Model.AdvPositions();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                return list;
            }
        }
        public static UcAsp.Net.Model.AdvPositions GetAdvPositions(string advname)
        {
            UcAsp.Net.Model.AdvPositions adv = GetListAdvPositions.FirstOrDefault(o => o.AdvPositionName.ToUpper() == advname.ToUpper());
            return adv;
        }
        /// <summary>
        /// 分类页面广告
        /// </summary>
        /// <param name="advname"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UcAsp.Net.Model.AdvPositions GetAdvPositions(string advname, string categoriesid)
        {
            UcAsp.Net.Model.AdvPositions adv = GetListAdvCategoriesPositions(categoriesid).FirstOrDefault(o => o.AdvPositionName.ToUpper() == advname.ToUpper());
            return adv;
        }
        /// <summary>
        /// 内容页面广告
        /// </summary>
        /// <param name="advname"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UcAsp.Net.Model.AdvPositions GetAdvMessagePositions(string advname, string id)
        {
            UcAsp.Net.Model.AdvPositions adv = GetListAdvMessagePositions(id).FirstOrDefault(o => o.AdvPositionName.ToUpper() == advname.ToUpper());
            return adv;
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SaveAdvPositions(UcAsp.Net.Model.AdvPositions model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\AdvPositions.xml");
            string xmlpath = ("root/AdvPositions");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["AdvPositionName"].InnerText == model.AdvPositionName)
                {
                    xns["AdvHtml"].InnerText = model.AdvHtml;
                }
            }
            xmlDoc.Save(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\AdvPositions.xml");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }
        public static List<UcAsp.Net.Model.AdvPositions> GetListAdvMessagePositions(string id)
        {
            List<UcAsp.Net.Model.AdvPositions> list = new List<UcAsp.Net.Model.AdvPositions>();

            if (!File.Exists(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id + "\\AdvPositions.xml"))
            {
                return list;
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id + "\\AdvPositions.xml");
            string xmlpath = ("root/AdvPositions");
            if (xmlDoc.SelectNodes(xmlpath).Count != 0)
            {
                XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                foreach (XmlNode xns in xmlList)
                {
                    UcAsp.Net.Model.AdvPositions model = new UcAsp.Net.Model.AdvPositions();

                    foreach (XmlNode xn in xns)
                    {
                        XmlToObject(model, xn.Name, xn.InnerText);
                    }
                    list.Add(model);
                }
            }
            return list;
        }
        public static List<UcAsp.Net.Model.AdvPositions> GetListAdvCategoriesPositions(string id)
        {
            List<UcAsp.Net.Model.AdvPositions> list = new List<UcAsp.Net.Model.AdvPositions>();

            if (!File.Exists(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id + "\\AdvPositions.xml"))
            {
                return list;
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id + "\\AdvPositions.xml");
            string xmlpath = ("root/AdvPositions");
            if (xmlDoc.SelectNodes(xmlpath).Count != 0)
            {
                XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                foreach (XmlNode xns in xmlList)
                {
                    UcAsp.Net.Model.AdvPositions model = new UcAsp.Net.Model.AdvPositions();

                    foreach (XmlNode xn in xns)
                    {
                        XmlToObject(model, xn.Name, xn.InnerText);
                    }
                    list.Add(model);
                }
            }
            return list;
        }
        public static bool SaveAdvCategoriesPositions(UcAsp.Net.Model.AdvPositions model, string id)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id + "\\AdvPositions.xml");
            string xmlpath = ("root/AdvPositions");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
            foreach (XmlNode xns in xmlList)
            {
                if (xns["AdvPositionName"].InnerText == model.AdvPositionName)
                {
                    xns["AdvHtml"].InnerText = model.AdvHtml;
                }
            }
            xmlDoc.Save(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\Categories\\" + id + "\\AdvPositions.xml");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }
        public static bool SaveAdvMessagePositions(UcAsp.Net.Model.AdvPositions model, string id)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id + "\\AdvPositions.xml");
            string xmlpath = ("root/AdvPositions");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
            foreach (XmlNode xns in xmlList)
            {
                if (xns["AdvPositionName"].InnerText == model.AdvPositionName)
                {
                    xns["AdvHtml"].InnerText = model.AdvHtml;
                }
            }
            xmlDoc.Save(path + "\\themes\\" + SettingConfig.GetSiteSetting.Theme + "\\config\\ShowMessage\\" + id + "\\AdvPositions.xml");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }
    }
}